package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sapling.api.model.ApiOutbuound;
import sapling.api.model.ApiPush;
import sapling.dao.*;
import sapling.model.*;
import sapling.push.JiguangPush2;
import sapling.utils.Msg;

import java.util.*;

/**
 * Created by Administrator on 2017/8/7.
 */
@RestController
@RequestMapping("/api/push")
public class PushApi {
    @Autowired
    TreeDao treeDao;
    @Autowired
    OutboundOrderDao outboundOrderDao;
    @Autowired
    MarkorderDao markorderDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    TreeWellDao treeWellDao;

    @ApiOperation(value = "获取待办任务", notes = "获取待办任务第一个List是价格产地待录入，第二个是出库待审批，第三个是死苗待确认")
    @GetMapping("/gettask")
    public Msg<ApiPush> gettask() {
        ApiPush apiPush = new ApiPush();
        List<Markorder> markorders1=new ArrayList<>();
        //待出库
        List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(0);
        List<ApiOutbuound> outStatus2 = new ArrayList<>();
        List<List<Tree>> lists = new ArrayList<>();
        for(OutboundOrder out:outStatus){
            ApiOutbuound apiout=new ApiOutbuound();
            int number=0;
            List<Tree> treeList=new ArrayList<>();
            for (Tree value:out.getTrees()) {
                if(Tree.TYPE_TREE_AWAIT_OUT==value.getSalesState()){
                    treeList.add(value);
                    number++;
                }
            }
            String name="";
            if(treeList.size()>3){
                for(int i=0;i<3;i++){
                    Tree tree=treeList.get(i);
                    name+=tree.getSerialNumber();
                }
                name+="...";
            }else{
                for(Tree tree:treeList){
                    name+=tree.getSerialNumber();
                }
            }
            apiout.setName(name);
            lists.add(treeList);
            apiout.setTreesList(treeList);
            if(number>0){
                apiout.setQuantity(number);
                outStatus2.add(apiout);
            }
        }
        List<Markorder> markorderlist5 = markorderDao.findBySalestate(5);
        //标记死苗
        List<Markorder> markorders3 = new ArrayList<>();
        for(Markorder mark:markorderlist5){
            Markorder markoder=new Markorder();
            int number=0;
            List<Tree> trees5=new ArrayList<>();
            for (Tree value:mark.getTreesList()) {
                if(Tree.TYPE_TREE_AWAIT_DIE==value.getSalesState()){
                    trees5.add(value);
                    number++;
                }
            }
            String diename="";
            if(trees5.size()>3){
                for(int i=0;i<3;i++){
                    Tree tree=trees5.get(i);
                    TreeWell treeWell = treeWellDao.findbytree(tree.getTree_id());
                    if(treeWell!=null){
                        diename+=treeWell.getLandMass().getAreaLocation().getSerialNumber()+treeWell.getLandMass().getSerialNumber()+"-"+treeWell.getSerialNumber()+";";
                    }
                }
                diename+="...";
            }else{
                for(Tree tree:trees5){
                    TreeWell treeWell = treeWellDao.findbytree(tree.getTree_id());
                    if(treeWell!=null){
                        diename+=treeWell.getLandMass().getAreaLocation().getSerialNumber()+treeWell.getLandMass().getSerialNumber()+"-"+treeWell.getSerialNumber()+";";
                    }
                }
            }
            markoder.setName(diename);
            markoder.setTreesList(trees5);
            if(number>0){
                markoder.setQuantity(number);
                markorders3.add(markoder);
            }
        }
        //入库价格录入
        List<String> serlist = treeDao.putserialList();
        List<Tree> trees = treeDao.putserialTreeList();
        Collections.sort(serlist);
        Collections.sort(trees, new Comparator<Tree>() {
            @Override
            public int compare(Tree o1, Tree o2) {
                return o1.getSerialNumber().compareTo(o2.getSerialNumber());
            }
        });

        for(String ser:serlist){
            Markorder markorder = new Markorder();
            List<Tree> treelist=new ArrayList<>();
            for(Tree tree:trees){
                if(tree.getSerialNumber().equals(ser)){
                    treelist.add(tree);
                }
            }
            markorder.setQuantity(treelist.size());
            markorder.setTreesList(treelist);
            String name = getOutName(treelist);
            name=(name==null?"":name);
            markorder.setName(name);
            markorders1.add(markorder);
        }
        apiPush.setPuttee(markorders1);
        apiPush.setDietee(markorders3);
        apiPush.setOuttee(outStatus2);
        return Msg.newMsg(apiPush);
    }

    public String getOutName(List<Tree> treelist) {
        String name="";
        if (treelist.size() > 3) {
            for (int i = 0; i < 3; i++) {
                Tree tree = treelist.get(i);
                name+=tree.getSename();
            }
            name += "...";
        } else {
            for (Tree tree : treelist) {
                name+=tree.getSename();
            }
        }
        return name;
    }

    @ApiOperation(value = "tuisong", notes = "tuisong")
    @GetMapping("/jpush")
    public void jpush() {
        Role role = roleDao.findOneByName("finance");
        for (User user : role.getUserList()) {
            JiguangPush2.jiguangPush(user.getUsername(), "出库待审批");
        }
    }
    @GetMapping("/setTeasename")
    public String setTeasename(){
        List<Tree> trees = treeDao.putserialTreeList();
        for(Tree tree:trees){
            if(tree.getSename()!=null){
                continue;
            }
            long tree_well_id = tree.getTree_well_id();
            TreeWell treeWell = treeWellDao.findOne(tree_well_id);
            if(treeWell!=null) {
                tree.setSename(treeWell.getLandMass().getAreaLocation().getSerialNumber() + treeWell.getLandMass().getSerialNumber() + "-" + treeWell.getSerialNumber());
            }else{
                tree.setSename("");
            }
            treeDao.saveAndFlush(tree);
        }
        return "success";
    }


}
